#include "Communicate.h"
#include "data/slide_show.hpp"

Communicate::~Communicate()
{
	delete m_parent;
}
Communicate::Communicate(const wxString& title)
	: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(1440, 900))
{
	m_parent = new wxPanel(this, wxID_ANY);

	wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);

	m_lp = new LeftPanel(m_parent);
	m_rp = new RightPanel(m_parent);

	hbox->Add(m_lp, 42, wxEXPAND | wxALL, 2);
	hbox->Add(m_rp, 58, wxEXPAND | wxALL, 2);

	m_parent->SetSizer(hbox);

	wxMenuBar *menubar = new wxMenuBar;

	wxMenu *file = new wxMenu;
	file->Append(wxID_NEW, wxT("&New"));
	Connect(wxID_NEW, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Communicate::on_new));
	file->Append(wxID_OPEN, wxT("&Open"));
	Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Communicate::on_open));
	file->Append(wxID_SAVE, wxT("&Save"));
	Connect(wxID_SAVE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Communicate::on_save));
	menubar->Append(file, wxT("&File"));

	SetMenuBar(menubar);

	this->Centre();
	this->SetMinSize(wxSize(1440,900));
}

void Communicate::on_open( wxCommandEvent& WXUNUSED(e) )
{
	wxFileDialog * openFileDialog = new wxFileDialog(this);

	wxString file_name;
	if (openFileDialog->ShowModal() == wxID_OK)
	{
		file_name = openFileDialog->GetPath(); 
		std::string f = file_name; 
		m_lp->clear_ui();
		g_slides.clear();
		g_slides.load_from_xml(f);
	}

	m_lp->set_slide_ui();
}
void Communicate::on_new( wxCommandEvent& WXUNUSED(e) )
{
	wxMessageDialog *dial = new wxMessageDialog(0, wxT("Are you sure?"), wxT("New"), wxYES_NO | wxICON_ASTERISK);
	int re = -1;
	re = dial->ShowModal();
	if( re == wxID_YES )
	{
		m_lp->clear_ui();
		g_slides.clear();
	}
}
void Communicate::on_save( wxCommandEvent& WXUNUSED(e) )
{
	wxFileDialog * save_file = new wxFileDialog(this, _("Save level file"), "", "",
                           "level or xml files (*.level;*.xml)|*.level;*.xml", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);

	wxString file_name; 
	if (save_file->ShowModal() == wxID_OK)
	{
		file_name = save_file->GetPath(); 
		std::string f = file_name;
		g_slides.save_to_file(f);
	}

	delete save_file;
}